home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / sml_nj / cml-098.lha / cml-0.9.8 / examples / ex-sieve.sml < prev    next >
Encoding:
Text File  |  1990-11-01  |  654 b   |  32 lines

  1. (* ex-sieve.sml
  2.  *
  3.  * COPYRIGHT (c) 1990 by John H. Reppy.  See COPYRIGHT file for details.
  4.  *
  5.  * A simple stream-style program to generate primes.  This uses ex-counter.sml.
  6.  *)
  7.  
  8. (* BEGIN EXAMPLE *)
  9. fun sieve () = let
  10.       val primes = channel ()
  11.       fun filter (p, inCh) = let
  12.     val outCh = channel()
  13.     fun loop () = let val i = accept inCh
  14.           in
  15.         if ((i mod p) <> 0) then send (outCh, i) else ();
  16.         loop ()
  17.           end
  18.     in
  19.       spawn loop;
  20.       outCh
  21.     end
  22.       fun head ch = let val p = accept ch
  23.         in
  24.           send (primes, p);
  25.           head (filter (p, ch))
  26.         end
  27.       in
  28.     spawn (fn () => head (counter 2));
  29.     primes
  30.       end
  31. (* END EXAMPLE *)
  32.